//
//  AppDelegate.m
//  carcareIOS
//
//  Created by ileo on 15/6/1.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "AppDelegate.h"
#import "ScreenMgr.h"
#import "SystemHelper.h"
#import "AIUserInfo.h"
#import "UserHelper.h"
#import "CommentHelper.h"
#import "NotificationHelper.h"
#import <WebKit/WebKit.h>
#import "ShareCenter.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    // Override point for customization after application launch.
    
    [SYSTEMHELPER configuration];
    [SCREEN config];
    
//    if (SYSTEMHELPER.isFirstTime) {
        [SCREEN startWelcomeScreen];
//    }else
    
    //判断是否由远程消息通知触发应用程序启动
    if ([launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]) {
    }
    // Override point for customization after application launch.
    [[ShareCenter sharedInstance] configHandlers:@[[WXHandler handleWithAppID:@"wx179d32fb6c5ed7e6"],[QQHandler handleWithAppID:@"1101986483"]]];
    
#pragma mark - userAgent
    NSString *userAgent = [[[[UIWebView alloc] init] stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"] stringByAppendingString:[NSString stringWithFormat:@" icz/iOS/%@",__APP_V]];
    [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":userAgent}];
    
    return YES;
}

#pragma mark - openURL ios9以上
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{
//    [EasyAliPay appOpenURL:url callBack:nil];
    return [[ShareCenter sharedInstance] handleOpenURL:url];
}

#pragma mark - openURL ios9以下
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
//    [EasyAliPay appOpenURL:url callBack:nil];
    return [[ShareCenter sharedInstance] handleOpenURL:url];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(id)annotation{
//    [EasyAliPay appOpenURL:url callBack:nil];
    return [[ShareCenter sharedInstance] handleOpenURL:url];
}
#pragma mark - -

#pragma mark - - -
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
    //获取终端设备标识，这个标识需要通过接口发送到服务器端，服务器端推送消息到APNS时需要知道终端的标识，APNS通过注册的终端标识找到终端设备。
    [NotificationHelper dealWithDeviceToken:deviceToken];
}

-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{
    [NotificationHelper dealWithFailError:error];
}

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
    //在此处理接收到的消息。
    [SYSTEMHELPER readAllNotification];
    [NotificationHelper dealWithReceiveInfo:userInfo];
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    [[NSNotificationCenter defaultCenter] postNotificationName:APP_WAKE_FROM_BG object:nil];
    [SYSTEMHELPER readAllNotification];
    if (USERINFO.isLogin) {
        [CommentHelper startTime];
    }
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    [CommentHelper stopTime];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
